<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>微信JS-SDK Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
</head>
<body>
111
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="../js/demo.js"></script>
<script src="../js/jweixin-1.0.0.js"></script>
<script src="../js/jquery.min.js"></script>

<script type="application/javascript">
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    var request = new Object();
    request = GetRequest();
//	alert(request.code);
	console.log(request.code);

    wx.config({
        debug : false,
        appId : 'wxfb43ab061dba7e3c',
        timestamp : "1488789271",
        nonceStr : 'hdjhnppf7qkeg8emzehfiuynklyt943j',
        signature : '36de7b0a19d21c5c407e308cd1d2639372a8653b',
        jsApiList : [ 'chooseWXPay' ]
    });

	//获取accessToken openid
    $.ajax({
        type: "GET",
//            dataType: "json",
        url: "https://www.51jbm.com/mwechat/api/getAccessToken",
        contentType: "application/json",
        data:{code:request.code},
        error : function(){
        },
        success : function(data){
            if(data) {
//                alert(data);
                getPrepayId(data.openid);
            } else {
                alert(data);
            }
        }
    });


    function getPrepayId(openid) {
        var param = {
            nonce_str: "hdjhnppf7qkeg8emzehfiuynklyt943j",
            body: "EVOP",
            out_trade_no: "20170306113730",
            fee_type: 'CNY',
            total_fee: 1,
            spbill_create_ip: '192.168.14.209',
            notify_url: 'http://www.51jbm.com/mwechat/api/pay',
            trade_type: 'JSAPI',
            detail:"test",
            attach:"1231",
            openid: openid
        };
        $.ajax({
            type: "POST",
//            dataType: "json",
            url: "https://www.51jbm.com/mwechat/api/unifiedOrder",
            contentType: "application/json",
            data: JSON.stringify(param),
            error: function () {
            },
            success: function (data) {
                if (data) {
                    getPaySign(data.nonce_str,data.prepay_id);
                } else {
                    alert(3);
                }
            }
        });
    }

    function getPaySign(noncestr,prepayId) {
        var  param = {
            timeStamp:"1488789271",
            nonceStr:noncestr,
            prepayId:prepayId
        };
        $.ajax({
            type: "GET",
//            dataType: "json",
            url: "https://www.51jbm.com/mwechat/api/payAuth",
            contentType: "application/json",
            data: param,
            error: function () {
            },
            success: function (data) {
                if (data) {
                    wxPay(data.packageWithPrepayId,data.paySign,data.nonceStr);
                } else {
                    alert(3);
                }
            }
        });
    }

    function wxPay(packageWithPrepayId,paySign,nonceStr) {
//        var param = {
//            timestamp: '1488531319',
//            nonceStr: 'c8q1mg7xvg7uc6mnaluhbt27872y3qib',
//            prepayId: prepay_id
//        };
//        $.ajax({
//            type: "GET",
////            dataType: "json",
//            url: "https://www.51jbm.com/mwechat/api/payAuth",
//            contentType: "application/json",
//            data: param,
//            error: function () {
//            },
//            success: function (data) {
//                if (data) {

                    wx.chooseWXPay({
                        timestamp: "1488789271", // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                        nonceStr: nonceStr, // 支付签名随机串，不长于 32 位
                        package: packageWithPrepayId, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
                        signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                        paySign: paySign, // 支付签名
                        success: function (res) {
                            // 支付成功后的回调函数
                            alert('pay success');
                        }
                    })
//                } else {
//                    alert(3);
//                }
////            }
//        });
        //alert(prepay_id);
    }

</script>

</html>
